﻿using Qy.Core;
using SqlSugar;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Qy.Scenario;

[AppService]
public class UserGroupService : QyRepository<UserGroup>
{
    private readonly UserService _userService;
    public UserGroupService(UserService userService, ApplicationContext applicationContext) : base(applicationContext)
    {
        _userService = userService;
    }

    public async Task<List<UserGroup>> GetListAllAsync()
    {
        return await GetAllListAsync("Id DESC");
    }
    public async Task<PagedInfo<UserGroup>> GetPageListAsync(QueryUserGroup parm)
    {
        return await Queryable()
                    .OrderBy(data => data.Id, OrderByType.Desc)
                    .Select(data => new UserGroup
                    {
                        Id = data.Id,
                        GroupName = data.GroupName,
                        Remark = data.Remark,
                    })
                    .ToPageAsync(parm);
    }

    public async Task<UserGroup> GetOne(int? id)
    {
        List<UserGroup> userGroups = await GetListAllAsync();
        return userGroups?.Find(x => x.Id == id);
    }

    public async Task<bool> GetByGroupNameAsync(string groupName)
    {        
       return await Queryable().AnyAsync(data=>data.GroupName== groupName);
    }

    public async Task<bool> DeleteDelCache(int id)
    {
        if (await _userService.GetByGidAsync(id))
            return false;
        else 
            return await DeleteAndDelCacheAsync(id) > 0;
    }
}
